/**
 * javascript for 核心模块
 * mvvm -- model view-model view  # vue.js avalon.js
 * mvc -- model view controller # angular </> 断崖 1.x --> 2.x typescript > javascript ms  less -> css
 * module - controller
 */

var app = angular.module("main", []);

/*
<html ng-app="main"> spa -> single page application
ng-app//application 1
ng-controller // controller 1+

ioc --> 依赖注入 -- string -> service
*/

app.factory('service', function(){
	return {
		kao: function(){alert("aaaa");}
	};
});

//1. m -> v defineProperty, dirty-data 语义上不能修改原有语义 
//var ppp = "123"; --> setValue(ppp, "123"); 1000+ ioc audit.ext.js

//2. v -> m DOM element listener keydown/keyup click/dblclick change
/*
$compile --> var $compile = function(str) {
   return function(scope) {
     alert(str);
     alert(scope);
   };
}

闭包-->持有变量 引用计数 GC 0->1->2->1

c("<input ng-model='aaaa'/>")($scope);
c("<input ng-model='aaaa'/>")($scope);
$xxx -> angular service
$$xxxx -> angular private service, undocument api
*/

app.controller("le-core", ["$scope", "$http", "service", "$compile", function($scope, $http, s, c){
	console.log(".......");
	$scope.aaaa = "涂飞平";
	$scope.sexs = [{v: '', display: '请选择'}, {v: 'M', display: '男'}, {v: 'F', display: '女'}];
	$scope.users = {
		name: 'sunnysssssssssss',
		age: 38,
		sex: '',
		phone: '2222222'
	};
	$scope.show=function(){
		alert($scope.user.sex);
	};
	$scope.click=s.kao;
	$("#stub").html(c("<input ng-model='aaaa'/>")($scope));
}]);

app.directive("myLoading", function(){
	return {
		restrict: 'A', //A:attribute, E:element, C: class, M: comment
		link: function(scope, element, attrs) {
			//element-jqLite $("element")
			element.on("click", function(){alert("xxxxx");});
			element.css({height: 100, width: 100, background: '#ccc'});
			scope.$watch("aaaa", function(v){
				console.log(v);
                element.css("background", v);
			});
		}
	};
});

app.filter("lefilter", function(){
	return function(oldvalue) {
        if (/^(\d{4})(\d{2})(\d{2})$/.test(oldvalue)) {
            return RegExp.$1 + "年" + RegExp.$2 + "月" + RegExp.$3 + "日";
        }
		return "[" + oldvalue + "]";
	};
});
//auto-focus -> autoFocus
//compile: pre, post
// link --> compile.post jquery->jqLite selector
app.directive('autoFocus', function() {
	return {
			restrict : 'ECA',
			link : function(scope, element, attrs) {
				element[0].focus();
			}
		};
});